﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Data;
using HHMMS.Core.Common;
using System.Web;
using System.Text.RegularExpressions;
using HHMMS.Core.Model;
using Bingosoft.Data;

namespace System
{
    /// <summary>
    /// 实用工具方法。
    /// </summary>
    public static class Util
    {
        /// <summary>
        /// 获取当前应用程序的根路径，该路径包括域名以及端口。
        /// </summary>
        public static string ContextPath
        {
            get
            {
                return System.Web.HttpContext.Current.Request.GetContextPath();
            }
        }

        /// <summary>
        /// 读取配置文件
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static T GetConfig<T>(string key, T defaultValue)
        {
            string str = ConfigurationManager.AppSettings[key];
            return str.ConvertTo<T>(defaultValue);
        }

        /// <summary>
        /// 获取指定对象指定属性值并以指定类型返回。
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="propertyName"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static T Bind<T>(object obj, string propertyName, T defaultValue)
        {
            return obj.GetValue(propertyName, defaultValue);
        }

        /// <summary>
        /// 获取指定对象指定属性值并以字符形式返回。
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static string Bind(object obj, string propertyName)
        {
            return obj.GetValue(propertyName, "");
        }

        /// <summary>
        /// html格式转换为文本。
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string HtmlToText(string html)
        {
            html = Regex.Replace(html, "<br[^>]+>", "\n");
            html = Regex.Replace(html, "&nbsp;", " ");
            return html;
        }

        /// <summary>
        /// 将GridView数据导出为Excel。
        /// </summary>
        /// <param name="gridView"></param>
        /// <param name="fileName">文件名，如"temp.xls"</param>
        /// <param name="exportAll">是否导出所有，默认只导出当前页</param>
        public static void ExportGridView(GridView gridView, string fileName, bool? exportAll = false, params int[] hideColIndex)
        {
            DataTable result = new DataTable();

            //添加列
            int i = -1;
            foreach (DataControlField col in gridView.Columns)
            {
                i++;
                if ((hideColIndex != null && hideColIndex.Length > 0 && i.In<int>(hideColIndex)) || !col.Visible)
                    continue;

                result.Columns.Add(col.HeaderText, typeof(string));
            }

            //添加行
            if (exportAll.HasValue && exportAll.Value)
            {
                gridView.AllowPaging = false;
                gridView.DataBind();
            }
            foreach (GridViewRow gvRow in gridView.Rows)
            {
                if (gvRow.RowType != DataControlRowType.DataRow)
                    continue;

                DataRow newRow = result.NewRow();
                i = -1;
                foreach (DataControlField col in gridView.Columns)
                {
                    i++;
                    if ((hideColIndex != null && hideColIndex.Length > 0 && i.In<int>(hideColIndex)) || !col.Visible)
                        continue;

                    if (!string.IsNullOrEmpty(gvRow.Cells[i].Text))
                    {
                        newRow[col.HeaderText] = gridView.Page.Server.HtmlDecode(HtmlToText(gvRow.Cells[i].Text));
                    }
                    else
                    {
                        Literal litText = gvRow.Cells[i].FindControl<Literal>("Text");
                        if (litText != null)
                        {
                            newRow[col.HeaderText] = HtmlToText(litText.Text);
                        }
                    }
                }
                result.Rows.Add(newRow);
            }
            if (exportAll.HasValue && exportAll.Value)
            {
                gridView.AllowPaging = true;
                gridView.DataBind();
            }

            //输出至客户端
            XlsWriter.CreateXls(result, fileName);
        }
    }
}
